//
//  DetailViewController.m
//  waiMai
//
//  Created by LONG on 16/4/12.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "DetailViewController.h"
#import "DetailTitleView.h"//头部的那块view
#import "DetaFoodViewController.h"//点菜
#import "EvaluateViewController.h"//评价
#import "MerchantViewController.h"//商家

@interface DetailViewController ()<DetailTitleViewDelegate,UIScrollViewDelegate>

//头部的选项卡
@property(nonatomic,strong) DetailTitleView *titleView;

//滚动条
@property(nonatomic,strong) UIScrollView *scrollView;

//大数组，子控制器的
@property(nonatomic,strong) NSMutableArray *childViews;

@end

@implementation DetailViewController

- (NSMutableArray *)childViews
{
    if(_childViews==nil)
    {
        _childViews =[NSMutableArray array];
    }
    return _childViews;
}

- (UIScrollView *)scrollView
{
    if(_scrollView==nil)
    {
       
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _titleView.frame.size.height, WIDTH, HEIGHT - _titleView.frame.size.height)];
        _scrollView.delegate=self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.contentSize = CGSizeMake(3 * WIDTH, 0);
        _scrollView.bounces = NO;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}


- (DetailTitleView *)titleView
{
    if(_titleView==nil)
    {
        _titleView =[DetailTitleView new];
        _titleView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _titleView.layer.borderWidth = 1.0;
        _titleView.delegate=self;
        _titleView.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
        [self.view addSubview:_titleView];
        
    }
    return _titleView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationItem.title = _myTitle;
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //nav底部线消失
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(MYColor)];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate =(id)self;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    
    self.view.backgroundColor = UIColorFromRGB(0xffffff);
    
    
    //添加头部的view
    [self titleView];
    
    //添加scrollView
    [self scrollView];
    
    //添加各子控制器
    [self setupChildVcs];

}
#pragma mark 添加各子控制器
- (void)setupChildVcs
{
    //点菜控制器
    DetaFoodViewController *foodVc =[DetaFoodViewController new];
    [self addChildViewController:foodVc];
    [self.childViews addObject:foodVc.view];
    
    //评价控制器
    EvaluateViewController *evaluateVc =[EvaluateViewController new];

    [self addChildViewController:evaluateVc];
    [self.childViews addObject:evaluateVc.view];
    
    //商家控制器
    MerchantViewController *merchantVc =[MerchantViewController new];
    [self addChildViewController:merchantVc];
    [self.childViews addObject:merchantVc.view];

    
    for(int i=0;i<self.childViews.count;i++)
    {
        UIView *childV = self.childViews[i];
        CGFloat childVX = WIDTH * i ;
        childV.frame = CGRectMake(childVX, 0, WIDTH, self.view.frame.size.height - _titleView.frame.size.height);
        [_scrollView addSubview:childV];
        
    }
    
    
}


#pragma mark 滚动条
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    [self.view endEditing:YES];
    
    
    if(scrollView==_scrollView)
    {
        if(_scrollView.contentOffset.x / WIDTH ==0)
        {
            [_titleView wanerSelected:0];
        }
        else if (_scrollView.contentOffset.x / WIDTH ==1)
        {
            [_titleView wanerSelected:1];
        }else if (_scrollView.contentOffset.x / WIDTH ==2)
        {
            [_titleView wanerSelected:2];
        }
        
        
    }
}

#pragma mark titleView的方法
- (void)titleView:(DetailTitleView *)titleView scollToIndex:(NSInteger)tagIndex
{
    
    
    [_scrollView setContentOffset:CGPointMake(tagIndex * WIDTH, 0) animated:YES];
    
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];

    
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
    
}

@end
